#!/usr/bin/env bash

##########################################################################
# THIS MODULE IS USED TO CREATE INSTALLATION SCRIPS TO ADD TO THE DATABASE
##########################################################################

# COMMON FUNCTIONS FOR APPIMAGES AND ARCHIVES/PROGRAMS/SCRIPTS (OPTIONS 0 AND 2)

function _template_create_dirs_and_ask_for_source() {
	mkdir -p ./am-scripts ./am-scripts/"$arch" ./am-scripts/portable-linux-apps.github.io/apps ./am-scripts/portable-linux-apps.github.io/icons
	# DOWNLOAD THE TEMPLATE AND CONVERT THE ARGUMENT INTO A COMMAND
	if [ "$templatetype" == 0 ]; then
		wget -q "$AMREPO"/templates/AM-SAMPLE-AppImage -O ./am-scripts/"$arch"/"$arg"
	elif [ "$templatetype" == 2 ]; then
		wget -q "$AMREPO"/templates/AM-SAMPLE-Archive -O ./am-scripts/"$arch"/"$arg"
	fi
	sed -i "s/SAMPLE/$arg/g" ./am-scripts/"$arch"/"$arg"
	echo "-----------------------------------------------------------------------"
}

function _template_if_github_source() {
	RESPONSE=$(echo "$RESPONSE" | sed 's#https://github.com/##g' | cut -f1,2 -d'/')
	sed -i "s#REPLACETHIS#$RESPONSE#g" ./am-scripts/"$arch"/"$arg"
	# GET THE DESCRIPTION OF THIS REPOSITORY TO ADD IN ALL CREATED FILES
	COMMENT=$(curl $HeaderAuthWithGITPAT https://api.github.com/repos/"$RESPONSE" 2>/dev/null | grep description | sed 's/"description": "//' | sed 's/",//' | cut -c 3-)
	# CREATE A WEBPAGE FOR https://portable-linux-apps.github.io CONTAINING ALL THE INFO ABOUT THIS APP
	echo "# $(echo "$arg" | tr '[:lower:]' '[:upper:]')" >> ./am-scripts/portable-linux-apps.github.io/apps/"$arg".md
	echo -e "\n $COMMENT\n\n SITE: https://github.com/$RESPONSE\n\n | [Applications](https://portable-linux-apps.github.io/apps.html) | [Home](https://portable-linux-apps.github.io)\n | --- | --- |" >> ./am-scripts/portable-linux-apps.github.io/apps/"$arg".md &&
	# CREATE A NEW LINE FOR THE APPLICATION'S LIST
	echo "◆ $arg : $COMMENT" >> ./am-scripts/list
	echo "-----------------------------------------------------------------------"
	# CHECK THE DOWNLOAD LINK, THE ONE THAT POINTS TO THE LATEST RELEASE, NOTE THAT IT MAY BE A DEVELOPMENT VERSION, ADD "/latest" AFTER "/releases" TO POINT TO THE LAST STABLE VERSION
	q="'"
	if [ "$templatetype" == 0 ]; then
		FUNCTION='curl -Ls https://api.github.com/repos/'"$RESPONSE"'/releases | sed '"$q"'s/[()",{} ]/\\n/g'"$q"' | grep -oi "https.*mage$" | grep -vi "i386\\|i686\\|aarch64\\|arm64\\|armv7l" | head -1'
	elif [ "$templatetype" == 2 ]; then
		FUNCTION='curl -Ls https://api.github.com/repos/'"$RESPONSE"'/releases | sed '"$q"'s/[()",{} ]/\\n/g'"$q"' | grep -oi "https.*" | grep -vi "i386\\|i686\\|aarch64\\|arm64\\|armv7l" | head -1'
	fi
	sed -i "s#FUNCTION)#$FUNCTION)#g" ./am-scripts/"$arch"/"$arg"
}

function _template_test_github_url_if_torsocks_exists() {
	GHURLPREVIEW=$(eval "$GHURLPREVIEW_COMMAND")
	if [ -z "$GHURLPREVIEW" ]; then
		if command -v torsocks 1>/dev/null; then
			GHURLPREVIEW="torsocks $GHURLPREVIEW_COMMAND"
			eval "$GHURLPREVIEW"
		fi
	else
		echo "$GHURLPREVIEW"
	fi
}

function _template_then_github_source() {

	_template_if_github_source

	# Set the release as "latest" or keep it generic
	read -r -p ' Latest release (y) or a generic one (N or leave blank)?' yn
	case $yn in
	'Y'|'y')
		setlatest="/latest"
		sed -i 's#/releases #/releases/latest #g' ./am-scripts/"$arch"/"$arg"
		;;
	''|'N'|'n'|*)
		setlatest=""
		;;
	esac
	echo "-----------------------------------------------------------------------"
	# Check if the URL is correct
	read -r -p ' Do you wish to check the link (Y,n)?' yn
	case $yn in
	'N'|'n')
		;;
	'Y'|'y'|*)
		if [ "$templatetype" == 0 ]; then
			echo ""
			GHURLPREVIEW_COMMAND="curl -Ls $HeaderAuthWithGITPAT https://api.github.com/repos/$RESPONSE/releases$setlatest | sed 's/[()\",{} ]/\n/g' | grep -oi \"https.*mage$\" | grep -vi \"i386\|i686\|aarch64\|arm64\|armv7l\" | head -1"
			_template_test_github_url_if_torsocks_exists
		elif [ "$templatetype" == 2 ]; then
			echo ""
			GHURLPREVIEW_COMMAND="curl -Ls $HeaderAuthWithGITPAT https://api.github.com/repos/$RESPONSE/releases$setlatest | sed 's/[()\",{} ]/\n/g' | grep -oi \"https.*\" | grep -vi \"i386\|i686\|aarch64\|arm64\|armv7l\" | head -1"
			_template_test_github_url_if_torsocks_exists
		fi
		echo -e "\n The URL above is an example of what both\n the install and update scripts will point to.\n"
		;;
	esac
	echo "-----------------------------------------------------------------------"
	# Add/remove keywords or leave blank to skip
	read -r -p ' If correct, press "ENTER", 1 to add keywords and 2 to remove keywords: ' response
	case "$response" in
	1)
		read -r -ep ' URL must contain ("x64", "x86_64"... or leave blank): ' response
		case "$response" in
		'')
			;;
		*)
			sed -i "s# head -1# grep -i \"$response\" | head -1#g" ./am-scripts/"$arch"/"$arg"
			read -r -p ' Do you wish to check the link for the last time (Y,n)?' yn
			case $yn in
			'N'|'n')
				;;
			'Y'|'y'|*)
				if [ "$templatetype" == 0 ]; then
					echo ""
					GHURLPREVIEW_COMMAND="curl -Ls $HeaderAuthWithGITPAT https://api.github.com/repos/$RESPONSE/releases$setlatest | sed 's/[()\",{} ]/\n/g' | grep -oi \"https.*mage$\" | grep -vi \"i386\|i686\|aarch64\|arm64\|armv7l\" | grep -i \"$response\" | head -1"
					_template_test_github_url_if_torsocks_exists
				elif [ "$templatetype" == 2 ]; then
					echo ""
					GHURLPREVIEW_COMMAND="curl -Ls $HeaderAuthWithGITPAT https://api.github.com/repos/$RESPONSE/releases$setlatest | sed 's/[()\",{} ]/\n/g' | grep -oi \"https.*\" | grep -vi \"i386\|i686\|aarch64\|arm64\|armv7l\" | grep -i \"$response\" | head -1"
					_template_test_github_url_if_torsocks_exists
				fi
				echo -e "\n The URL above is an example of what both\n the install and update scripts will point to.\n"
				;;
			esac
			;;
		esac
		;;
	2)
		read -r -ep ' URL must NOT contain ("txt", "ARM"... or leave blank): ' response
		case "$response" in
		'')
			;;
		*)
			sed -i "s# head -1# grep -v \"$response\" | head -1#g" ./am-scripts/"$arch"/"$arg"
			read -r -p ' Do you wish to check the link for the last time (Y,n)?' yn
			case $yn in
			'N'|'n')
				;;
			'Y'|'y'|*)
				if [ "$templatetype" == 0 ]; then
					echo ""
					GHURLPREVIEW_COMMAND="curl -Ls $HeaderAuthWithGITPAT https://api.github.com/repos/$RESPONSE/releases$setlatest | sed 's/[()\",{} ]/\n/g' | grep -oi \"https.*mage$\" | grep -vi \"i386\|i686\|aarch64\|arm64\|armv7l\" | grep -v \"$response\" | head -1"
					_template_test_github_url_if_torsocks_exists
				elif [ "$templatetype" == 2 ]; then
					echo ""
					GHURLPREVIEW_COMMAND="curl -Ls $HeaderAuthWithGITPAT https://api.github.com/repos/$RESPONSE/releases$setlatest | sed 's/[()\",{} ]/\n/g' | grep -oi \"https.*\" | grep -vi \"i386\|i686\|aarch64\|arm64\|armv7l\" | grep -v \"$response\" | head -1"
					_template_test_github_url_if_torsocks_exists
				fi
				echo -e "\n The URL above is an example of what both\n the install and update scripts will point to.\n"
				;;
			esac
			;;
		esac
		;;
	''|*)
		;;
	esac
}

function _template_custom_download_url_as_version_variable() {
	# IF YOU CAN, USE A ONE-LINE COMMAND TO DOWNLOAD THE PROGRAM
	read -r -ep " USE A ONE-LINE COMMAND TO CHECK THE URL TO THE PROGRAM OR THE VERSION $(echo -e '\n\n if the URL is fixed, simply add the "echo" command at the beginning\n\n :') " DOWNLOADURL
	case "$DOWNLOADURL" in
	*)
		mv ./am-scripts/"$arch"/"$arg" ./am-scripts/"$arch"/"$arg".old
		sed -n '1,14p' ./am-scripts/"$arch"/"$arg".old >> ./am-scripts/"$arch"/"$arg"
		echo 'version=$('$DOWNLOADURL')' >> ./am-scripts/"$arch"/"$arg"
		sed -n '16,35p' ./am-scripts/"$arch"/"$arg".old >> ./am-scripts/"$arch"/"$arg"
		echo 'version=$('$DOWNLOADURL')' >> ./am-scripts/"$arch"/"$arg"
		sed -n '37,132p' ./am-scripts/"$arch"/"$arg".old >> ./am-scripts/"$arch"/"$arg"
		rm -f ./am-scripts/"$arch"/"$arg".old
		echo "-----------------------------------------------------------------------"
		;;
	esac
}

function _template_use_wget_to_download_a_linear_url_or_a_command() {
	# FOR "WGET", ADD A LINEAR DOWNLOAD URL OR CHECK THE URL USING THE "$( ... )" SYNTAX
	read -r -ep " ◆ FOR \"WGET\", ADD A COMMAND AS \"\$( ... )\" OR A LINEAR DOWNLOAD URL $(echo -e '\n : ')" DOWNLOADURL
	case "$DOWNLOADURL" in
	*)
		mv ./am-scripts/"$arch"/"$arg" ./am-scripts/"$arch"/"$arg".old
		sed -n '1,15p' ./am-scripts/"$arch"/"$arg".old >> ./am-scripts/"$arch"/"$arg"
		echo 'wget "'$DOWNLOADURL'" || exit 1' >> ./am-scripts/"$arch"/"$arg"
		sed -n '17,40p' ./am-scripts/"$arch"/"$arg".old >> ./am-scripts/"$arch"/"$arg"
		if grep -q "appimage" ./am-scripts/"$arch"/"$arg".old; then
			echo '	[ -e ../*.zsync ] && wget "$version.zsync" 2>/dev/null || { wget "'$DOWNLOADURL'" || exit 1; }' >> ./am-scripts/"$arch"/"$arg"
		else
			echo '	wget "'$DOWNLOADURL'" || exit 1' >> ./am-scripts/"$arch"/"$arg"
		fi
		sed -n '42,150p' ./am-scripts/"$arch"/"$arg".old >> ./am-scripts/"$arch"/"$arg"
		rm -f ./am-scripts/"$arch"/"$arg".old
		echo "-----------------------------------------------------------------------"
		;;
	esac
}

function _template_help_by_repology() {
	# IF A DOWNLOAD URL IS LINEAR, USE https://repology.org
	read -r -ep " Do you want to use repology.org to identify version (y,N)? " yn
	case "$yn" in
	'Y'|'y')
		repology=$(wget -q https://repology.org/project/$arg/versions -O - | grep -i "new.*version" | head -1 | tr '><' '\n' | grep "^[0-9]")
		repology_command="wget -q https://repology.org/project/$arg/versions -O - | grep -i \"new.*version\" | head -1 | tr '><' '\\n' | grep \"^[0-9]\""
		if [ -z "$repology" ]; then
			repology=$(wget -q https://repology.org/project/$arg/versions -O - | grep -i "uniq.*version" | head -1 | tr '><' '\n' | grep "^[0-9]")
			repology_command="wget -q https://repology.org/project/$arg/versions -O - | grep -i \"uniq.*version\" | head -1 | tr '><' '\\n' | grep \"^[0-9]\""
			echo ""; echo " $repology"; echo ""
			mv ./am-scripts/"$arch"/"$arg" ./am-scripts/"$arch"/"$arg".old
			sed -n '1,14p' ./am-scripts/"$arch"/"$arg".old >> ./am-scripts/"$arch"/"$arg"
			echo 'version=$('$repology_command')' >> ./am-scripts/"$arch"/"$arg"
			sed -n '16,35p' ./am-scripts/"$arch"/"$arg".old >> ./am-scripts/"$arch"/"$arg"
			echo 'version=$('$repology_command')' >> ./am-scripts/"$arch"/"$arg"
			sed -n '37,132p' ./am-scripts/"$arch"/"$arg".old >> ./am-scripts/"$arch"/"$arg"
			rm -f ./am-scripts/"$arch"/"$arg".old
		else
			echo ""; echo " $repology"; echo ""
			mv ./am-scripts/"$arch"/"$arg" ./am-scripts/"$arch"/"$arg".old
			sed -n '1,14p' ./am-scripts/"$arch"/"$arg".old >> ./am-scripts/"$arch"/"$arg"
			echo 'version=$('$repology_command')' >> ./am-scripts/"$arch"/"$arg"
			sed -n '16,35p' ./am-scripts/"$arch"/"$arg".old >> ./am-scripts/"$arch"/"$arg"
			echo 'version=$('$repology_command')' >> ./am-scripts/"$arch"/"$arg"
			sed -n '37,132p' ./am-scripts/"$arch"/"$arg".old >> ./am-scripts/"$arch"/"$arg"
			rm -f ./am-scripts/"$arch"/"$arg".old
		fi
		if [ "$templatetype" != 1 ]; then
			_template_use_wget_to_download_a_linear_url_or_a_command
		fi
		;;
	'N'|'n'|*)
		echo "-----------------------------------------------------------------------"
		_template_custom_download_url_as_version_variable
		;;
	esac
}

function _template_if_sourceforge() {
	sed -i "s#REPLACETHIS#$RESPONSE#g" ./am-scripts/"$arch"/"$arg"
	# ADD PROJECTNAME
	read -r -ep " ◆ ADD THE NAME OF THE PROJECT OR LEAVE BLANK IF ITS THE SAME $(echo -e '\n : ')" projectname
	case "$projectname" in
	'')	SOURCEFORGE="https://sourceforge.net/p/$arg/activity/feed";;
	*)	SOURCEFORGE="https://sourceforge.net/p/$projectname/activity/feed";;
	esac
	echo "-----------------------------------------------------------------------"
	# FILE TYPE
	if [ "$templatetype" == 0 ]; then
		filetype="appimage"
	elif [ "$templatetype" == 2 ]; then
		read -r -ep " ◆ ADD an univoque keyword for the file (for example \"tar.gz\", \"linux\"):" filekeyword
		case "$filekeyword" in
		*) filetype="$filekeyword";;
		esac
	fi
	# ADD A DESCRIPTION FOR THIS APPLICATION
	read -r -ep " ◆ ADD A BRIEF DESCRIPTION FOR THIS APPLICATION $(echo -e '\n : ')" COMMENT
	case "$COMMENT" in
	*)
		# CREATE A WEBPAGE FOR https://portable-linux-apps.github.io CONTAINING ALL THE INFO ABOUT THIS APP
		echo "# $(echo "$arg" | tr '[:lower:]' '[:upper:]')" >> ./am-scripts/portable-linux-apps.github.io/apps/"$arg".md
		echo -e "\n $COMMENT\n\n SITE: $RESPONSE\n\n | [Applications](https://portable-linux-apps.github.io/apps.html) | [Home](https://portable-linux-apps.github.io)\n | --- | --- |" >> ./am-scripts/portable-linux-apps.github.io/apps/"$arg".md &&
		# CREATE A NEW LINE FOR THE APPLICATION'S LIST
		echo "◆ $arg : $COMMENT" >> ./am-scripts/list
		;;
	esac
	echo "-----------------------------------------------------------------------"
	if curl --head --silent --fail $(curl -Ls $SOURCEFORGE | grep -Eo "(http|https)://[a-zA-Z0-9./?=_%:-]*" | grep -i "$filetype" | grep -v '%' | head -1) 1> /dev/null; then
		mv ./am-scripts/"$arch"/"$arg" ./am-scripts/"$arch"/"$arg".old
		sed -n '1,14p' ./am-scripts/"$arch"/"$arg".old >> ./am-scripts/"$arch"/"$arg"
		echo "version=\$(curl -Ls $SOURCEFORGE | grep -Eo \"(http|https)://[a-zA-Z0-9./?=_%:-]*\" | grep -i \"$filetype\" | grep -v '%' | head -1)" >> ./am-scripts/"$arch"/"$arg"
		sed -n '16,35p' ./am-scripts/"$arch"/"$arg".old >> ./am-scripts/"$arch"/"$arg"
		echo "version=\$(curl -Ls $SOURCEFORGE | grep -Eo \"(http|https)://[a-zA-Z0-9./?=_%:-]*\" | grep -i \"$filetype\" | grep -v '%' | head -1)" >> ./am-scripts/"$arch"/"$arg"
		sed -n '37,132p' ./am-scripts/"$arch"/"$arg".old >> ./am-scripts/"$arch"/"$arg"
		rm -f ./am-scripts/"$arch"/"$arg".old
		curl -Ls $SOURCEFORGE | grep -Eo "(http|https)://[a-zA-Z0-9./?=_%:-]*" | grep -i "$filetype" | grep -v '%' | head -1
		echo -e "\n The URL above is an example of what both\n the install and update scripts will point to.\n"
	else
		_template_custom_download_url_as_version_variable
	fi
	sed -i 's/*mage/*/g' ./am-scripts/"$arch"/"$arg"
}

function _template_description_if_hosted_elsewhere() {
	sed -i "s#REPLACETHIS#$RESPONSE#g" ./am-scripts/"$arch"/"$arg"
	# ADD A DESCRIPTION FOR THIS APPLICATION
	read -r -ep " ◆ ADD A BRIEF DESCRIPTION FOR THIS APPLICATION $(echo -e '\n : ')" COMMENT
	case "$COMMENT" in
	*)
		# CREATE A WEBPAGE FOR https://portable-linux-apps.github.io CONTAINING ALL THE INFO ABOUT THIS APP
		echo "# $(echo "$arg" | tr '[:lower:]' '[:upper:]')" >> ./am-scripts/portable-linux-apps.github.io/apps/"$arg".md
		echo -e "\n $COMMENT\n\n SITE: $RESPONSE\n\n | [Applications](https://portable-linux-apps.github.io/apps.html) | [Home](https://portable-linux-apps.github.io)\n | --- | --- |" >> ./am-scripts/portable-linux-apps.github.io/apps/"$arg".md &&
		# CREATE A NEW LINE FOR THE APPLICATION'S LIST
		echo "◆ $arg : $COMMENT" >> ./am-scripts/list
		;;
	esac
	echo "-----------------------------------------------------------------------"
}

# Function to add a generic .desktop file and an icon to the installation script (option 2)
function _template_generate_desktop_file() {
	sed -i "s/#printf/printf/g" ./am-scripts/"$arch"/"$arg"
	cat <<-'HEREDOC' >> ./am-scripts/"$arch"/"$arg"

	# ICON
	mkdir -p icons
	wget ICONURL -O ./icons/"$APP" 2> /dev/null

	# LAUNCHER
	echo "[Desktop Entry]
	Name=APPNAME
	Exec=$APP
	Icon=/opt/$APP/icons/$APP
	Type=Application
	Terminal=false
	Categories=YOURCATEGORY;" > /usr/local/share/applications/"$APP"-AM.desktop
	HEREDOC
}

# Function to add a category to our custom .desktop file (options 2 and 3)
function _template_desktop_file_select_category() {
	echo -e " LIST OF CATEGORIES:\n 0) AudioVideo\n 1) Audio\n 2) Video\n 3) Development\n 4) Education\n 5) Game\n 6) Graphics\n 7) Network\n 8) Office\n 9) Science\n 10) Settings\n 11) System\n 12) Utility (default)\n "
	read -r -p " CHOOSE A CATEGORY : " response
	case "$response" in
		0)	sed -i "s/YOURCATEGORY/AudioVideo/g" ./am-scripts/"$arch"/"$arg";;
		1)	sed -i "s/YOURCATEGORY/Audio/g" ./am-scripts/"$arch"/"$arg";; 
		2)	sed -i "s/YOURCATEGORY/Video/g" ./am-scripts/"$arch"/"$arg";; 
		3)	sed -i "s/YOURCATEGORY/Development/g" ./am-scripts/"$arch"/"$arg";;
		4)	sed -i "s/YOURCATEGORY/Education/g" ./am-scripts/"$arch"/"$arg";; 
		5)	sed -i "s/YOURCATEGORY/Game/g" ./am-scripts/"$arch"/"$arg";; 
		6)	sed -i "s/YOURCATEGORY/Graphics/g" ./am-scripts/"$arch"/"$arg";; 
		7)	sed -i "s/YOURCATEGORY/Network/g" ./am-scripts/"$arch"/"$arg";;
		8)	sed -i "s/YOURCATEGORY/Office/g" ./am-scripts/"$arch"/"$arg";; 
		9)	sed -i "s/YOURCATEGORY/Science/g" ./am-scripts/"$arch"/"$arg";; 
		10)	sed -i "s/YOURCATEGORY/Settings/g" ./am-scripts/"$arch"/"$arg";; 
		11)	sed -i "s/YOURCATEGORY/System/g" ./am-scripts/"$arch"/"$arg";;
		12|*)	sed -i "s/YOURCATEGORY/Utility/g" ./am-scripts/"$arch"/"$arg";;
	esac
	echo "-----------------------------------------------------------------------"
}

#####################################
# LET CREATE OUR INSTALLATION SCRIPTS
#####################################

case $2 in
'')
	echo " USAGE: $AMCLI $1 [ARGUMENT]"; exit
	;;
esac

while [ -n "$1" ]; do
	rm -f "$AMCACHEDIR"/template-args
	echo "$@" | tr ' ' '\n' >> "$AMCACHEDIR"/template-args && echo STOP >> "$AMCACHEDIR"/template-args
	ARGS=$(tail -n +2 "$AMCACHEDIR"/template-args)
	for arg in $ARGS; do
		if [ "$arg" == STOP ]; then
			exit
		else
			arg=$(echo "$arg" | tr '[:upper:]' '[:lower:]')
			case $arg in
			*)
				cd "$SCRIPTDIR" || return
				echo "##############################################################"
				echo ""
				echo " Create a template for \"$arg\"" | tr '[:lower:]' '[:upper:]'
				echo ""
				echo "--------------------------------------------------------------"
				echo "" 
				echo " EACH MODEL IS BASED ON A DIFFERENT METHOD OF BUILDING/UPDATING THE PROGRAM."
				echo ""
				echo " PLEASE, SELECT A TEMPLATE FOR \"$(echo "$arg" | tr '[:lower:]' '[:upper:]')\":"
				echo ""
				echo "  0) APPIMAGE, FROM ANY WEBSITE (BETTER IF FROM GITHUB.COM)"
				echo "  1) APPIMAGE ON-THE-FLY, USING A DEDICATED SCRIPT"
				echo "  2) ANY ARCHIVE/BINARY/SCRIPT FROM ANY WEBSITE"
				echo '  3) WEBAPP BASED ON A FIREFOX PROFILE (REQUIRES "FIREFOX" IN $PATH)'
				echo ""
				read -r -p " WHAT KIND OF PROGRAM DO YOU WANT TO WRITE A SCRIPT FOR? : " templatetype
				case "$templatetype" in

				0) # APPIMAGE (ANY)

					_template_create_dirs_and_ask_for_source

					# ADD THE NAME OF THE SOURCE
					read -r -ep " ◆ ADD THE URL OF THE SITE (BETTER IF IT COME FROM GITHUB), FOR EXAMPLE: $(echo -e '\n\n - if from github.com, use "https://github.com/USER/PROJECT"\n\n - if from other sources, use "https://www.SITENAME.com" or something\n\n URL: ')" RESPONSE
					case "$RESPONSE" in
					*)
						if echo "$RESPONSE" | grep -q "https://github.com"; then
							_template_then_github_source
						elif echo "$RESPONSE" | grep -q "sourceforge"; then
							_template_if_sourceforge
						elif ! echo "$RESPONSE" | grep -q "://"; then
							_template_then_github_source
						else
							_template_description_if_hosted_elsewhere
							_template_help_by_repology
						fi
					esac
					# END OF THIS FUNCTION
					echo "-----------------------------------------------------------------------"
					echo -e "\n All files are saved in $SCRIPTDIR/am-scripts\n"
					;;

				1) # CREATE AN APPIMAGE ON-THE-FLY
					mkdir -p ./am-scripts ./am-scripts/"$arch" ./am-scripts/portable-linux-apps.github.io/apps ./am-scripts/portable-linux-apps.github.io/icons
					# DOWNLOAD THE TEMPLATE AND CONVERT THE ARGUMENT INTO A COMMAND
					wget -q "$AMREPO"/templates/AM-SAMPLE-AppImage -O ./am-scripts/"$arch"/"$arg"
					sed -i "s/SAMPLE/$arg/g" ./am-scripts/"$arch"/"$arg"

					_template_description_if_hosted_elsewhere
					_template_help_by_repology

					# ADD A LINEAR DOWNLOAD URL
					read -r -ep " ◆ ADD A LINEAR DOWNLOAD URL FOR THE SCRIPT OR LEAVE BLANK $(echo -e '\n : ')" dlurl
					case "$dlurl" in
					'')
						sed -i 's#wget "$version"#wget "'"$AMREPO"'/appimage-bulder-scripts/'"$arch"'/$APP.sh" \&\& chmod a+x "$APP".sh \&\& ./"$APP".sh#g' ./am-scripts/"$arch"/"$arg"
					;;
					*)
						sed -i 's#wget "$version"#wget "'"$dlurl"'" -O '"$arg"'.sh && chmod '"$arg"'.sh && ./'"$arg"'.sh#g' ./am-scripts/"$arch"/"$arg"
					;;
					esac
					# END OF THIS FUNCTION
					echo "-----------------------------------------------------------------------"
					echo -e "\n All files are saved in $SCRIPTDIR/am-scripts\n"
					;;

				2) # DOWNLOAD ANY ARCHIVE

					_template_create_dirs_and_ask_for_source

					# ADD THE NAME OF THE SOURCE
					read -r -ep " ◆ ADD THE URL OF THE SITE (BETTER IF IT COME FROM GITHUB), FOR EXAMPLE: $(echo -e '\n\n - if from github.com, use "https://github.com/USER/PROJECT"\n\n - if from other sources, use "https://www.SITENAME.com" or something\n\n URL: ')" RESPONSE
					case "$RESPONSE" in
					*)
						if echo "$RESPONSE" | grep -q "https://github.com"; then
							_template_then_github_source
						elif echo "$RESPONSE" | grep -q "sourceforge"; then
							_template_if_sourceforge
						elif ! echo "$RESPONSE" | grep -q "://"; then
							_template_then_github_source
						else
							_template_description_if_hosted_elsewhere
							_template_help_by_repology
						fi
					esac
					# LAUNCHER AND ICON (OPTIONAL)
					read -r -p " ◆ Do you wish to add a launcher and a icon (y,N)? " yn
					case $yn in
					'Y'|'y')
						# ADD LAUNCHER AND ICON REFERENCES TO THE INSTALLATION SCRIPT
						_template_generate_desktop_file

						# ENTER THE URL OF THE ICON THAT WILL APPEAR IN THE MENU
						read -r -ep " ◆ COPY/PASTE THE URL OF THE ICON HERE $(echo -e '\n URL: ')" ICONURL
						case "$ICONURL" in
						'')	sed -i "s#ICONURL#https://portable-linux-apps.github.io/icons/$arg.png#g" ./am-scripts/"$arch"/"$arg"
							;;
						*)	sed -i "s#ICONURL#$ICONURL#g" ./am-scripts/"$arch"/"$arg"
							wget -q -P ./am-scripts/portable-linux-apps.github.io/icons/ "$ICONURL"
							;;
						esac
						echo "-----------------------------------------------------------------------"
						# BINARY NAME
						read -r -ep " ◆ NAME OF THE BINARY FILE (LEAVE BLANK IF IT IS \"$arg\"): " BINARY
						case "$BINARY" in
						'')
							;;
						*)	sed -i 's#ln -s /opt/$APP/$APP #ln -s /opt/$APP/'"$BINARY"' #g' ./am-scripts/"$arch"/"$arg"
							;;
						esac
						echo "-----------------------------------------------------------------------"
						# APPNAME
						read -r -ep ' ◆ NAME OF THE APP (for the "Name" entry of the .desktop file): ' APPNAME
						case "$APPNAME" in
						*)
							sed -i "s#APPNAME#$APPNAME#g" ./am-scripts/"$arch"/"$arg"
							;;
						esac
						echo "-----------------------------------------------------------------------"
						# PUT THE APP INTO A CATEGORY, THIS IS NEEDED FOR OUR CUSTOM DESKTOP FILE
						_template_desktop_file_select_category
						;;
					'N'|'n'|*)
						echo "-----------------------------------------------------------------------"
						;;
					esac
					# END OF THIS FUNCTION
					echo -e "\n All files are saved in $SCRIPTDIR/am-scripts\n"
					;;

				3) # CREATE A CUSTOM FIREFOX PROFILE ("firefox" MUST BE IN "$PATH" TO MADE IT WORK)
					mkdir -p ./am-scripts ./am-scripts/"$arch" ./am-scripts/portable-linux-apps.github.io/apps ./am-scripts/portable-linux-apps.github.io/icons
					# DOWNLOAD THE TEMPLATE AND CONVERT THE ARGUMENT INTO A COMMAND
					wget -q "$AMREPO"/templates/AM-SAMPLE-Firefox-webapp -O ./am-scripts/"$arch"/"$arg" &&
					sed -i "s/SAMPLE/$arg/g" ./am-scripts/"$arch"/"$arg" &&
					# USE THE SUFFIX FROM NOW ON
					mv ./am-scripts/"$arch"/"$arg" ./am-scripts/"$arch"/"ffwa-$arg"
					arg="ffwa-$arg"
					echo "-----------------------------------------------------------------------"
					read -r -ep " ◆ NAME OF THE APP: " RESPONSE
					case "$RESPONSE" in
					*)
						sed -i "s#GIVEMEANAME#$RESPONSE#g" ./am-scripts/"$arch"/"$arg"
						# CREATE A WEBPAGE FOR https://portable-linux-apps.github.io CONTAINING ALL THE INFO ABOUT THIS APP
						echo "# $(echo "$arg" | tr '[:lower:]' '[:upper:]')" >> ./am-scripts/portable-linux-apps.github.io/apps/"$arg".md
						echo -e "\n WebApp & Firefox Profile for $RESPONSE.\n\n SITE: MYWEBSITE\n\n | [Applications](https://portable-linux-apps.github.io/apps.html) | [Home](https://portable-linux-apps.github.io)\n | --- | --- |" >> ./am-scripts/portable-linux-apps.github.io/apps/"$arg".md &&
						# CREATE A NEW LINE FOR THE APPLICATION'S LIST
						echo "◆ $arg : WebApp & Firefox Profile for $RESPONSE." >> ./am-scripts/list
					esac
					# PUT THE APP INTO A CATEGORY, THIS IS NEEDED FOR OUR CUSTOM DESKTOP FILE
					_template_desktop_file_select_category
					# ENTER THE URL OF THE SITE YOU WANT TO CREATE A CUSTOM FIREFOX PROFILE FOR
					read -r -ep " ◆ URL OF THE WEBAPP: " RESPONSE
					case "$RESPONSE" in
					*)
						sed -i "s#MYWEBSITE#$RESPONSE#g" ./am-scripts/"$arch"/"$arg"; sed -i "s#MYWEBSITE#$RESPONSE#g" ./am-scripts/portable-linux-apps.github.io/apps/"$arg".md
						;;
					esac 
					echo "-----------------------------------------------------------------------"
					# ENTER THE URL OF THE ICON THAT WILL APPEAR IN THE MENU
					read -r -ep " ◆ COPY/PASTE THE URL OF THE ICON HERE $(echo -e '\n URL: ')" RESPONSE
					case "$RESPONSE" in
					*)
						sed -i "s#YOURICONURL#$RESPONSE#g" ./am-scripts/"$arch"/"$arg"
						;;
					esac
					echo "-----------------------------------------------------------------------"
					# END OF THIS FUNCTION
					echo -e "\n All files are saved in $SCRIPTDIR/am-scripts\n"
					;;

				# NOTHING SELECTED
				*)
					echo -e "\n No valid argument was chosen: process aborted! \n" | tr '[:lower:]' '[:upper:]'; break
					;;
				esac
			esac
		fi
	done
done

shift
